﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using Oracle.DataAccess.Client;

namespace WebApplication.Account
{
    public partial class Register : System.Web.UI.Page
    {
        OracleConnection conn;
        OracleCommand command;
        OracleDataReader reader;
        private string password;

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = "~/Reserveren.aspx";
            conn = new OracleConnection();

            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["OracleConnectionString"].ToString();
            
            TextBox t = (TextBox) RegisterUserWizardStep.ContentTemplateContainer.FindControl("Username");
            t.Text = GetNewRFID();
            t.Enabled = false;
        }

        private string GetNewRFID() {
            List<String> rfidList = new List<String>();
            string sql = "SELECT RFID FROM RFID_COL WHERE RFID NOT IN(SELECT RFID FROM PERSOON) AND RFID NOT IN (SELECT RFID FROM KLANT)";
            command = new OracleCommand(sql, conn);
            conn.Open();

            reader = command.ExecuteReader();

            while(reader.Read()){
                rfidList.Add(reader[0].ToString());
            }

            conn.Close();

            return rfidList[0];
        }

        private string MaakWachtwoord(string rfid)
        {
            string ww = null;

            for (int i = 0; i < Math.Abs(rfid.GetHashCode()).ToString().Length; i++)
            {
                char bekent = Math.Abs(rfid.GetHashCode()).ToString()[i];
                char temp;
                if (bekent == '1' || bekent == '8' || bekent == '6' || bekent == '2' || bekent == '0')
                {
                    temp = Convert.ToChar(65 + bekent);
                }
                else
                {
                    temp = bekent;
                }
                ww += temp;
            }

            return ww.ToUpper();
        }

        public void sendmail(string rfid, string emailid, string password)
        {
            string siteurl = "http://localhost:49614/Activation.aspx";
            string smsg = "U heeft zich geregistreerd op de website van De Valkenhof, hieronder uw inloggegevens:<br>";
            smsg += "<br>RFID: " + rfid;
            smsg += "<br>Wachtwoord: " + password;
            smsg += "<br>Uw account is reeds nog niet geactiveerd, doe dat nu door op <a href=" + siteurl + "?rfid=" + rfid + "><u>DEZE</u></a> link te klikken.";
            smsg += "<br>Met vriendelijke groeten,<br><br><br>";
            smsg += "Management, Camping De Valkenhof";
            
           
                MailMessage message = new MailMessage("Camping.DeValkenhof@gmail.com", emailid);
                message.Subject = "Bevestiging registratie Camping De Valkenhof.";
                message.Body = smsg;
                message.IsBodyHtml = true;
                SmtpClient client = new SmtpClient();
                client.Port = 587; // Gmail works on this port 587, anderen 25
                client.Host = "smtp.gmail.com";
                System.Net.NetworkCredential nc = new System.Net.NetworkCredential("Camping.DeValkenhof@gmail.com", "onraadbaar");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = nc;
                client.Send(message);
        }

            protected void RegisterButtonClick(object sender, EventArgs e)
            {
                TextBox tbusername = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("UserName");
                TextBox tbemail = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Email");
                TextBox tbnaam = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Naam");
                TextBox tbadres = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Address");
                TextBox tbpostcode = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Postcode");
                TextBox tbwoonplaats = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Woonplaats");
                TextBox tbtelefoonNummer = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Telefoonnummer");
                TextBox tbsofinummer = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Sofinummer");
                TextBox tbrekeningnummer = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Rekeningnummer");

                string sql = "INSERT INTO Klant_Betalend VALUES ('" + tbusername.Text + "','" + tbnaam.Text + "','" + tbadres.Text + "','" + tbpostcode.Text + "','" + tbwoonplaats.Text + "','" + tbtelefoonNummer.Text + "','" + tbemail.Text + "','" + tbrekeningnummer.Text + "','" + tbsofinummer.Text + "', 0, 'NO')";
                
                password = MaakWachtwoord(tbusername.Text);
                string savePersoon = "INSERT INTO PERSOON VALUES('" + tbusername.Text + "','" + Reserveringssysteem.SHA1Hashing.MaakSHA1(password) + "','Klant_betalend')";
                command = new OracleCommand(savePersoon, conn);
                conn.Open();

                command.ExecuteNonQuery();

                conn.Close();

                command = new OracleCommand(sql, conn);

                conn.Open();

                command.ExecuteNonQuery();

                conn.Close();

                sendmail(tbusername.Text, tbemail.Text, password);

                Response.Redirect("~/Activeren.aspx");

            }
        }
    }

